package com.hcb.micro.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.BannerProduct;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.Product;
import com.hcb.micro.dto.ProductSeller;
import com.hcb.micro.service.IBannerProductService;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IProductSellerService;
import com.hcb.micro.service.IProductService;
import com.hcb.micro.service.IQqbsAccountService;

import net.sf.json.JSONObject;

/**
 * 
 * @author lynn
 * @date 2017年9月13日 上午10:12:56
 */
@Controller
@RequestMapping("bannerOfProduct")
public class BannerProductController extends BaseControllers{

	@Autowired
	IQqbsAccountService qqbsAccountService;
	
	@Autowired
	IMallService mallService;
	
	@Autowired
	IProductService productService;
	
	@Autowired
	IProductSellerService productSellerService;
	
	@Autowired
	IBannerProductService bannerProductService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		
		if(bodyInfo.get("productId") != null && !"".equals(bodyInfo.get("productId"))) {
			para.put("productId", Integer.valueOf(bodyInfo.getString("productId")));
		}
		if(bodyInfo.get("name") != null && !"".equals(bodyInfo.get("name"))) {
			para.put("name", bodyInfo.getString("name"));
		}
		
		List<Product> productListAll = productService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		
		List<Product> productList = productService.selectByPara(para);
		
		if(productList.size() <= 0){
			json.put("result", "1");
			json.put("description", "商品未创建");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(Product product:productList){
			Map<String,String> map = new HashMap<>();
			
			Map<String,Object> para2 = new HashMap<>();
			if(bodyInfo.get("bannerId") != null && !"".equals(bodyInfo.get("bannerId"))) {
				para2.put("bannerId", Integer.valueOf(bodyInfo.getString("bannerId")));
			}
			para2.put("productId", product.getId());
			List<BannerProduct> bpList = bannerProductService.selectByPara(para2);
			
			String isHave = "0";
			String isHaveValue = "未加入";
			if(bpList.size()>0){
				isHave = "1";
				isHaveValue = "已加入";
			}
			
			ProductSeller ps = productSellerService.selectByPrimaryKey(product.getProductSellerId());
			
			map.put("productId", product.getId()+"");
			map.put("productName", product.getName()+"");
			map.put("productImage", product.getImage1()+"");
			map.put("isHave", isHave);
			map.put("isHaveValue", isHaveValue);
			map.put("productSellerName", ps==null?"":ps.getName());
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取承接页商品列表成功");
		json.put("list", list);
		json.put("count", productListAll.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("productId") == null || "".equals(bodyInfo.get("productId"))){
			right = 0;
		}else if(bodyInfo.get("bannerId") == null || "".equals(bodyInfo.get("bannerId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Map<String,Object> para = new HashMap<>();
		
		if(bodyInfo.get("bannerId") != null && !"".equals(bodyInfo.get("bannerId"))) {
			para.put("bannerId", Integer.valueOf(bodyInfo.getString("bannerId")));
		}
		if(bodyInfo.get("productId") != null && !"".equals(bodyInfo.get("productId"))) {
			para.put("productId", Integer.valueOf(bodyInfo.getString("productId")));
		}
		
		List<BannerProduct> bpList = bannerProductService.selectByPara(para);
		
		if(bpList.size() <= 0){
			BannerProduct bp = new BannerProduct();
			bp.setBannerId(Integer.valueOf(bodyInfo.getString("bannerId")));
			bp.setProductId(Integer.valueOf(bodyInfo.getString("productId")));
			bp.setStatus((byte)1);
			bp.setCreateTime(new Date());
			
			Integer rs = bannerProductService.insertSelective(bp);
			if(rs == 0){
    			System.out.println("---新增承接页商品失败---");
    			json.put("result", "1");
    			json.put("description", "新增承接页商品失败");
    			return buildReqJsonObject(json);
    		}else{
    			System.out.println("---新增承接页商品成功---");
    		}
		}else{
			System.out.println("---新增承接页商品失败,本承接页已存在该商品---");
			json.put("result", "0");
			json.put("description", "本承接页已存在该商品");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "新增分类商品成功");
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("productId") == null || "".equals(bodyInfo.get("productId"))){
			right = 0;
		}else if(bodyInfo.get("bannerId") == null || "".equals(bodyInfo.get("bannerId"))){
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Map<String,Object> para = new HashMap<>();
		
		if(bodyInfo.get("bannerId") != null && !"".equals(bodyInfo.get("bannerId"))) {
			para.put("bannerId", Integer.valueOf(bodyInfo.getString("bannerId")));
		}
		if(bodyInfo.get("productId") != null && !"".equals(bodyInfo.get("productId"))) {
			para.put("productId", Integer.valueOf(bodyInfo.getString("productId")));
		}
		
		List<BannerProduct> bpList = bannerProductService.selectByPara(para);
		
		if(bpList.size() > 0){
			
			Integer rs = bannerProductService.deleteByPrimaryKey(bpList.get(0).getId());
			if(rs == 0){
    			System.out.println("---删除承接页商品失败---");
    			json.put("result", "1");
    			json.put("description", "删除承接页商品失败");
    			return buildReqJsonObject(json);
    		}else{
    			System.out.println("---删除承接页商品成功---");
    		}
		}else{
			System.out.println("---删除承接页商品失败,本承接页不存在该商品---");
			json.put("result", "0");
			json.put("description", "本承接页不存在该商品");
			return buildReqJsonObject(json);
		}
		
    	json.put("result", "0");
		json.put("description", "删除承接页商品成功");
		return buildReqJsonObject(json);
	}
}
